package com.eve.common.util;

import java.util.HashMap;
import java.util.ResourceBundle;

import com.eve.common.util.Constant;

/**
 * messageSource 获取校验文件信息
 * @author HXL
 *
 */
public class MessageUtils {

	private static  ResourceBundle rb; 
	/**
	 * 语言设置
	 */
	private static String  language; 
	/**
	 * 多语言设置
	 */
	private static HashMap<String, ResourceBundle> rbMap;
	
	static {
		rbMap = new HashMap<>();
		rbMap.put("zh_CN", ResourceBundle.getBundle("i18n.message_zh_CN"));
		rbMap.put("en_US", ResourceBundle.getBundle("i18n.message_en_US"));
		language = Constant.I18N;
		rb = rbMap.get(language);
	}
	/**
	 * 国际化
	 * 注：通过@Autowired private MessageSource messageSource无法获取
	 *
	 * @param result
	 * @return
	 */
	public static String getMessage(String result, Object[] params) {
	    /*ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
	    messageSource.setCacheSeconds(-1);
	    messageSource.setDefaultEncoding("UTF-8");
	    messageSource.setBasenames("i18n.message_" + language);*/
	    String message = "";
	    message = rb.getString(result);
	    /*try {
	    	Locale locale = new Locale(language);
	    	LocaleContextHolder.setLocale(locale);
	        locale = LocaleContextHolder.getLocale();
	        message = messageSource.getMessage(result, params, locale);
	    } catch (Exception e) {
	        message = result;
	    }*/
	    return message;

	}
	/**
	 * 修改语言 20191220
	 * @param text
	 * @return
	 */
	public static String changeLanguage(String text) {  
		if(rbMap.containsKey(text)) {
			language = text;
			rb = rbMap.get(language);		
		}
		return language;
	}  
}
